package com.milks.controller.shop;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.milks.entity.Users;
import com.milks.service.IUserService;
import com.milks.utils.RandomValidateCode;



@Controller
@RequestMapping("/account")
public class AccountController {
	
	private IUserService userService;

	public IUserService getUserService() {
		return userService;
	}
	@Resource
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login(Model model) {
		return "login";
	}
	
	/**
	 * 生成验证码图片
	 * @param request
	 * @param response
	 * @param session
	 * @param model
	 */
	@RequestMapping(value="/getVerifyCode",method=RequestMethod.GET)
	public void getVerifyCode(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model model) {
		
		response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
        response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        RandomValidateCode randomValidateCode = new RandomValidateCode();
        String randomString = "";
        try {
        	randomString =randomValidateCode.getRandcode(request, response);//输出图片方法
        } catch (Exception e) {
            e.printStackTrace();
        }
        //把生成的验证码放进去
        session.setAttribute("verifyCode", randomString);
	}
	/**
	 * 验证传递过来的验证码是否正确
	 * @param verifyCode
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/checkVerifyCode",method=RequestMethod.GET)
	@ResponseBody
	public String checkVerifyCode(String verifyCode,Model model,HttpSession session) {
		if (session.getAttribute("verifyCode").toString().equals(verifyCode)) {
			//session.removeAttribute("verifyCode");
			return "true";
		}
		return "false";
	}
	/**
	 * 登录
	 * @param uname
	 * @param password
	 * @param verifycode
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String login(String uname,String password,String verifycode,Model model,HttpSession session) {
		
		Users u = userService.login(uname, password);
		if (u!=null) {
			//model.addAttribute("loginUser", u.getId());
			session.setAttribute("loginUser", u.getName());
			return "redirect:/index/";
		}
		model.addAttribute("message", "用户名密码不正确");
		return "login";
	}
	/**
	 * 跳转到注册页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/register",method=RequestMethod.GET)
	public String register(Model model) {
		return "register";
	}
	/**
	 * 验证用户名是否已经存在
	 * @param username
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/checkUsername",method=RequestMethod.POST)
	@ResponseBody
	public List<String> checkUsername(String username,Model model) {
		Users users = userService.loadByUsername(username);
		List<String> list = new ArrayList<String>();
		if (users!=null) {
			list.add("true");list.add("该登录账号已经被占用，请换一个重试");
			return list;
		}
		list.add("false");list.add("username not exits");
		return list;
	}
	
	/**
	 * 验证用户名是否已经存在
	 * @param username
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/checkEmail",method=RequestMethod.GET)
	@ResponseBody
	public List<String> checkEmail(String email,Model model) {
		Users users = userService.loadByEmail(email);
		List<String> list = new ArrayList<String>();
		if (users!=null) {
			list.add("true");list.add("该邮箱已经被占用，请换一个重试");
			return list;
		}
		list.add("false");list.add("该邮箱可以使用");
		return list;
	}
	
	@RequestMapping("/register")
	public String register(String reg_user,String reg_passwd,String reg_email,String verifycode,Model model,HttpSession session) {
		Users u = new Users();
		u.setName(reg_user);u.setPassword(reg_passwd);u.setEmail(reg_email);
		userService.add(u);
		session.setAttribute("loginUser", u.getName());
		return "redirect:/index/";
	}
	
	@RequestMapping("/logout")
	public String logout(Model model,HttpSession session) {
		model.asMap().remove("loginUser");
		session.invalidate();
		return "redirect:/index/";
	}
	


}
